package com.oracle.oa.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.criterion.DetachedCriteria;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.LoginLogBiz;
import com.oracle.oa.biz.impl.LoginLogBizImpl;
import com.oracle.oa.biz.impl.OperatelogBizImpl;
import com.oracle.oa.entity.Loginlog;
import com.oracle.oa.entity.Userinfo;
import com.oracle.oa.util.PageBean;

public class LoginLogAction extends ActionSupport implements SessionAware {
	LoginLogBiz lBiz;
	public Loginlog loginlog = new Loginlog();
	public List<Loginlog> loginloglist=new ArrayList<Loginlog>();
	public PageBean pageBean=new PageBean();
	public String currentPage;
	public String url=null;
	public String startTime;
	public Map<String, Object> session = null;
	
	
//	List<Loginlog> logvalue=new ArrayList<Loginlog>();
	List logvalue=new ArrayList();
	


	public String save() {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Userinfo user=(Userinfo) session.get("user");
		if(session.get("error").equals("ok")){
			url="../admin_cg/system/index.jsp";
			loginlog.setIfsuccess(1);
			loginlog.setUserinfo(user);
			loginlog.setLoginuserip("127.0.0.1");
			loginlog.setLogintime(sdf.format(new Date()));
			lBiz.save(loginlog);
		}else if(session.get("error").equals("密码不正确!")){
			this.addActionError((String) session.get("error"));
			url="../admin_cg/system/login.jsp";
			loginlog.setIfsuccess(0);
			loginlog.setUserinfo(user);
			loginlog.setLoginuserip("127.0.0.1");
			loginlog.setLogintime(sdf.format(new Date()));
			lBiz.save(loginlog);
		}else{
			url="system/login.jsp";
		}
		return SUCCESS;
	}

	public String delete() {
		OperatelogBizImpl.add("删除", "删除登录日志");
		for (int i = 0; i < logvalue.size(); i++) {
			Loginlog l=new Loginlog();
			l=(Loginlog) lBiz.getById(Loginlog.class, Long.parseLong(logvalue.get(i).toString()));
			lBiz.delete(l);
		}
		/*for (Object lo : logvalue) {
			System.out.println(lo.toString());
		}*/
		getAll();
		return SUCCESS;
	}

	public String one() {

		return SUCCESS;
	}

	public String update() {

		return SUCCESS;
	}
	public String getAll(){
		System.out.println(currentPage+"1111111111111111111111111111111");
		Class clz=Loginlog.class;
		DetachedCriteria dc = DetachedCriteria.forClass(clz);
		lBiz.splitPage1(pageBean, dc, clz);
		int current=1;
		if(currentPage!=null){
			current=Integer.parseInt(currentPage);
			if(current<1){
				current=1;
			}
			if(current>pageBean.getTotalPage()){
				current=pageBean.getTotalPage();
			}
			pageBean.setCurrentPage(current);
		}
		lBiz.splitPage1(pageBean, dc, clz);
		loginloglist=pageBean.getList();
		
		url="../admin_cg/system/loginLogManager.jsp";
		return SUCCESS;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public Loginlog getLoginlog() {
		return loginlog;
	}

	public void setLoginlog(Loginlog loginlog) {
		this.loginlog = loginlog;
	}

	public List getLogvalue() {
		return logvalue;
	}

	public void setLogvalue(List logvalue) {
		this.logvalue = logvalue;
	}

	public List<Loginlog> getLoginloglist() {
		return loginloglist;
	}

	public void setLoginloglist(List<Loginlog> loginloglist) {
		this.loginloglist = loginloglist;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public LoginLogBiz getlBiz() {
		return lBiz;
	}

	public void setlBiz(LoginLogBiz lBiz) {
		this.lBiz = lBiz;
	}
	
}
